﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Shapes;

namespace FasterWPF
{
    public static class ContentControlExt
    {
        //Due to order of precedence issues, only set Background Color via styles 
        //(so that we can apply MouseOverColor triggers)
        public static void ChangeBackgroundColor(ContentControl button, Brush newColor)
        {
            if (button != null)
            {
                Style style = new Style(typeof(ContentControl), button.Style);

                Setter setter = new Setter();
                setter.Property = ContentControl.BackgroundProperty;
                setter.Value = newColor;
                style.Setters.Add(setter);

                button.Style = style;
            }
        }

        public static void ChangeBackgroundColor(List<ContentControl> buttons, Brush newColor)
        {
            if (buttons != null && buttons.Count > 0)
            {
                Style style = new Style(typeof(ContentControl), buttons[0].Style);

                Setter setter = new Setter();
                setter.Property = ContentControl.BackgroundProperty;
                setter.Value = newColor;
                style.Setters.Add(setter);

                foreach (ContentControl btn in buttons)
                {
                    btn.Style = style;
                }
            }
        }

        public static ContentControl SetMouseOverColorOnBorder(this ContentControl parentBorder, Brush brush)
        {
            Style style = new Style(typeof(ContentControl), parentBorder.Style);
            Trigger t = new Trigger();
            t.Property = ContentControl.IsMouseOverProperty;
            t.Value = true;
            Setter setter = new Setter();
            setter.Property = ContentControl.BackgroundProperty;
            setter.Value = brush;
            t.Setters.Add(setter);

            style.Triggers.Add(t); // Important: add a trigger before applying style
            parentBorder.Style = style;

            return parentBorder;
        }

        public static ContentControl SetMouseOverForeGroundOnBorder(this ContentControl parentBorder, Brush brush)
        {
            Style style = new Style(typeof(ContentControl), parentBorder.Style);
            Trigger t = new Trigger();
            t.Property = ContentControl.IsMouseOverProperty;
            t.Value = true;
            Setter setter = new Setter();
            setter.Property = TextBlock.ForegroundProperty;
            setter.Value = brush;
            t.Setters.Add(setter);

            style.Triggers.Add(t); // Important: add a trigger before applying style
            parentBorder.Style = style;

            return parentBorder;
        }

        /// <summary>
        /// This Initialize method is for explicit ContentControls ONLY.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="containerType"></param>
        /// <param name="borderBrush"></param>
        /// <param name="borderThickness"></param>
        /// <param name="background"></param>
        /// <param name="padding"></param>
        /// <param name="margin"></param>
        /// <returns></returns>
        public static ContentControl Initialize(this ContentControl source, double width, double height, ContainerType containerType, Brush borderBrush, Thickness borderThickness, Brush background, Thickness padding, Thickness margin)
        {
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.BorderBrush = borderBrush;
            source.BorderThickness = borderThickness;
            //source.CornerRadius = cornerRadius;
            source.Padding = padding;
            source.Margin = margin;

            ChangeBackgroundColor( source, background);

            sharedBorderInitializeCode(source, containerType);

            return source;
        }

        /// <summary>
        /// This Initialize method is for explicit ContentControls ONLY.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="containerType"></param>
        /// <param name="borderBrush"></param>
        /// <param name="borderThickness"></param>
        /// <param name="background"></param>
        /// <returns></returns>
        public static ContentControl Initialize(this ContentControl source, double width, double height, ContainerType containerType, Brush borderBrush, Thickness borderThickness, Brush background)
        {
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.BorderBrush = borderBrush;
            source.BorderThickness = borderThickness;

            ChangeBackgroundColor(source, background);

            sharedBorderInitializeCode(source, containerType);

            return source;
        }

        /// <summary>
        /// This Initialize method is for explicit ContentControls ONLY.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="containerType"></param>
        /// <param name="borderBrush"></param>
        /// <param name="borderThickness"></param>
        /// <param name="background"></param>
        /// <returns></returns>
        public static ContentControl Initialize(this ContentControl source, ContainerType containerType, Brush borderBrush, Thickness borderThickness, Brush background)
        {
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.BorderBrush = borderBrush;
            source.BorderThickness = borderThickness;

            ChangeBackgroundColor(source, background);

            sharedBorderInitializeCode(source, containerType);

            return source;
        }

        /// <summary>
        /// This Initialize method is for explicit ContentControls ONLY.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="containerType"></param>
        /// <param name="borderBrush"></param>
        /// <param name="borderThickness"></param>
        /// <returns></returns>
        public static ContentControl Initialize(this ContentControl source, ContainerType containerType, Brush borderBrush, Thickness borderThickness)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.BorderBrush = borderBrush;
            source.BorderThickness = borderThickness;
            
            sharedBorderInitializeCode(source, containerType);

            return source;
        }

        /// <summary>
        /// This Initialize method is for explicit ContentControls ONLY.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="containerType"></param>
        /// <returns></returns>
        public static ContentControl Initialize(this ContentControl source, ContainerType containerType)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            sharedBorderInitializeCode(source, containerType);

            return source;
        }

        //Private Methods
        private static void sharedBorderInitializeCode(ContentControl source, ContainerType containerType)
        {
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<ContentControl>(source);
            source.SetIComposite<ContentControl>(icomp);

            ContentControlParent gbsr = new ContentControlParent();
            source.StoreSetting<ContentControl>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<ContentControl>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });
        }                
    }//end of class
}//end of namespace
